package com.djl.seckilldemo.vo;

import com.djl.seckilldemo.IsMobile;
import com.djl.seckilldemo.util.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Required;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @program: seckill-demo
 * @description: 手机号码校验规则
 * @author: Mr.dai
 * @create: 2021-10-15 09:28
 **/

public class IsMobileValidator implements ConstraintValidator <IsMobile,String> {
    private  boolean required=false;
    @Override
    public void initialize(IsMobile constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (required){
            return ValidatorUtil.isMobile(value);
        }else {
            if (StringUtils.isEmpty(value)){
                return true;
            }else {
                return ValidatorUtil.isMobile(value);
            }
        }
    }
}
